package com.model.mapparser;

import com.core.RequestParams;
import com.model.user.User;
import com.model.user.UserGroup;
import com.model.user.UserRole;
import com.servicelayer.project.attributes.UserGroupServiceLayer;
import com.servicelayer.project.attributes.UserRoleServiceLayer;


public class UserMapParser {

    public static User fromMap ( RequestParams requestParams ) {

        if ( requestParams == null ) {
            return null;
        }


        Integer userRoleId = requestParams.integerAtIndexForKey(0, "userRole");

        Integer userGroupId = requestParams.integerAtIndexForKey(0, "userGroup");

        String name = requestParams.stringAtIndexForKey(0, "name");

        String description = requestParams.stringAtIndexForKey(0, "description");

        String mailAddress = requestParams.stringAtIndexForKey(0, "mailAddress");

        UserRoleServiceLayer userRoleServiceLayer = new UserRoleServiceLayer();
        UserRole userRole = (UserRole) userRoleServiceLayer.objectByUNID(userRoleId, null, null);

        UserGroupServiceLayer userGroupServiceLayer = new UserGroupServiceLayer();
        UserGroup userGroup = (UserGroup) userGroupServiceLayer.objectByUNID(userGroupId, null, null);


        User template = null;
        template = new User();
        template.setName(name);
        template.setDescription(description);
        template.setMailAddress(mailAddress);
        template.getUserCategory().setUserRole(userRole);
        template.getUserCategory().setUserGroup(userGroup);

        return template;
    }
}
